﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data;

public partial class service_forgetPwd : System.Web.UI.Page
{
    protected string tShowSuccess = "";
    protected string tel = "";
    protected string email = "";
    protected string ques = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bd();
        }
    }
    /// <summary>
    /// 接收编号
    /// </summary>
    private int Id
    {
        get
        {
            int id = 1;
            try
            {
                id = int.Parse(Request.QueryString["id"]);
            }
            catch
            {
                id = 1;
            }
            return id;
        }
    }
    /// <summary>
    /// 用户名
    /// </summary>
    private string Name
    {
        get
        {
            string name = "";
            try
            {
                if (Session["fname"] != null&&Session["fname"]!="")
                {
                    name = Session["fname"].ToString();
                } if (Request["user"] != null&&Request["user"]!="")
                {
                    name = Request["user"].ToString();
                }
            }
            catch
            {
                Response.Write("<script>alert('你还没有输入用户名');document.location='service_self.aspx';</script>");
                Response.End();
            }
            return name;
        }
    }
    protected void bd()
    {
        if (Id == 5)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = false;
            Panel5.Visible = true;
        }
        else if (Id == 2)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = false;
            Panel4.Visible = false;
            Panel5.Visible = false;
        }
        else if (Id == 3)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;
            Panel4.Visible = false;
            Panel5.Visible = false;
        }
        else if (Id == 4)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = true;
            Panel5.Visible = false;
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = false;
            Panel5.Visible = false;
        }
        //调用webservice根据用户名找回密码存入session
        FeeDBWS.FeeDBWS feedb = new FeeDBWS.FeeDBWS();
        string pwd = feedb.EMGet_PSD_WL(Name);  //找回密码
        //ClientScript.RegisterStartupScript(GetType(), "a", "alert('" + Name + "');", true);
        if (!pwd.Contains("密码"))
        {
            ClientScript.RegisterStartupScript(GetType(), "a", "alert('" + pwd + "');document.location='service_self.aspx';", true);
        }
        else
        {
            //判断用户是否存在
            pwd = pwd.Replace("密码", "");
            Session["pwd"] = pwd;
            //this.hidName.Value = Name;
            Session["fname"] = Name;
            DataSet ds1 = feedb.GetPwdByParam(0, "", Name, "");
            BakcPwd backpwd = null;

            foreach (DataRow dr in ds1.Tables[0].Rows)
            {
                backpwd = new BakcPwd();
                backpwd.Email = Tools.GetString(dr["email"]);
                backpwd.Tel = Tools.GetString(dr["tel"]);
                backpwd.Question = Tools.GetString(dr["question"]);
                backpwd.Answer = Tools.GetString(dr["answer"]);
            }
            if (backpwd != null)
            {
                if (!string.IsNullOrEmpty(backpwd.Tel))
                    tel = "<span class=\"font14 font-weight c8_width\"><a href=\"service_forgetPwd.aspx?id=2\">用密保手机找回密码</a></span><img src=\"images/ico_ok.jpg\" />&nbsp;已验证";
                else
                    tel = "<span class=\"font14 font-weight c8_width\">用密保手机找回密码</span><span class=\"red\">未验证</span>";
                if (!string.IsNullOrEmpty(backpwd.Email))
                {
                    email = "<span class=\"font14 font-weight c8_width\"><a href=\"service_forgetPwd.aspx?id=3\">用密保邮箱找回密码</a></span><img src=\"images/ico_ok.jpg\" />&nbsp;已验证";
                }
                else
                    email = "<span class=\"font14 font-weight c8_width\">用密保邮箱找回密码</span><span class=\"red\">未验证</span>";
                if (!string.IsNullOrEmpty(backpwd.Question) && !string.IsNullOrEmpty(backpwd.Answer))
                    ques = "<span class=\"font14 font-weight c8_width\"><a href=\"service_forgetPwd.aspx?id=4\">用密保问题找回密码</a></span><img src=\"images/ico_ok.jpg\" />&nbsp;已验证";
                else
                    ques = "<span class=\"font14 font-weight c8_width\">用密保问题找回密码</span><span class=\"red\">未验证</span>";

                DropDownList1.SelectedValue = backpwd.Question;
            }
            else
            {
                tel = "<span class=\"font14 font-weight c8_width\">用密保手机找回密码</span><span class=\"red\">未验证</span>";
                email = "<span class=\"font14 font-weight c8_width\">用密保邮箱找回密码</span><span class=\"red\">未验证</span>";
                ques = "<span class=\"font14 font-weight c8_width\">用密保问题找回密码</span><span class=\"red\">未验证</span>";
                ClientScript.RegisterStartupScript(GetType(), "a", "alert('您未绑定任何密保信息，请您致电4006708688');", true);
            }
        }
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        string tel = txtUTel.Text;
        string code = (string)Session["code"];
        if (code.Trim() == txtUTCode.Text.Trim())
        {
            try
            {
                FeeDBWS.FeeDBWS feedb = new FeeDBWS.FeeDBWS();
                DataSet bp = feedb.GetPwdByParam(1, Name, tel, "");
                if (bp.Tables[0].Rows.Count > 0)
                {
                    //ClientScript.RegisterStartupScript(GetType(), "a", "alert('" + Name + "');", true);
                    emoneybussiness.emoneybusiness service = new emoneybussiness.emoneybusiness();
                    service.SendMsg(tel, "您软件的密码为:" + Session["pwd"] + "，请妥善保管。如有疑问，请拨打 400-670-8688");
                    //feedb.GetValidateCodeForMobile(tel, "您软件的密码为:" + Session["pwd"] + "，请妥善保管。如有疑问，请拨打 400-670-8688");
                    tShowSuccess = "<li class=\"coffee font14 font-weight\">已发送至您的手机" + txtUTel.Text + "</li>";
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    Panel4.Visible = false;
                    Panel5.Visible = true;
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "a", "alert('请输入绑定手机号码');", true);
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "a", "alert('服务器忙，请重试');", true);
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "a", "alert('验证码输入错误');", true);
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string code = (string)Session["code"];
        if (code.Trim() == txtUECode.Text.Trim())
        {
            FeeDBWS.FeeDBWS feedb = new FeeDBWS.FeeDBWS();
            DataSet bp = feedb.GetPwdByParam(2, Name, txtUEOldEmail.Text, "");
            if (bp.Tables[0].Rows.Count > 0)
            {
                MailAddress MessageFrom = new MailAddress("service@emoney.cn"); //发件人邮箱地址 
                string MessageTo = txtUEOldEmail.Text; //收件人邮箱地址 
                string MessageSubject = "找回密码"; //邮件主题 
                string MessageBody = "尊敬的" + Name + "用户您好:\r\n我们收到了您提出的密码找回需求，您软件的密码为" + Session["pwd"] + "请妥善保管。\r\n您的支持就是我们最大的成功。\r\n如有疑问，请拨打 400-670-8688\r\n该邮件为后台自动发送，请不要回复该邮件"; //邮件内容 
                if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody))
                {

                    tShowSuccess = "<li class=\"coffee font14 font-weight\">已发送至您的邮箱" + txtUEOldEmail.Text + "</li>";
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    Panel4.Visible = false;
                    Panel5.Visible = true;
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "a", "alert('发送邮件失败');", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "a", "alert('请输入绑定邮箱');", true);
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "a", "alert('验证码输入错误');", true);
        }
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        string code = (string)Session["code"];
        if (code.Trim() == txtQCode.Text.Trim())
        {
            FeeDBWS.FeeDBWS feedb = new FeeDBWS.FeeDBWS();
            DataSet bp = feedb.GetPwdByParam(3, Name, txtUQuesion.Text, DropDownList1.SelectedValue);
            if (bp.Tables[0].Rows.Count > 0)
            {
                tShowSuccess = "<li class=\"coffee font14 font-weight\">您的软件密码为" + Session["pwd"] + "</li>";
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = true;
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "a", "alert('绑定问题回答错误，请重试');", true);
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "a", "alert('验证码输入错误');", true);
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.Hidden1.Value = DropDownList1.SelectedValue;
    }
    /// <summary> 
    /// 发送电子邮件 
    /// </summary> 
    /// <param name="MessageFrom">发件人邮箱地址</param> 
    /// <param name="MessageTo">收件人邮箱地址</param> 
    /// <param name="MessageSubject">邮件主题</param> 
    /// <param name="MessageBody">邮件内容</param> 
    /// <returns></returns> 
    protected bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
    {
        MailMessage message = new MailMessage();


        message.From = MessageFrom;
        message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发 
        message.Subject = MessageSubject;
        message.Body = MessageBody;
        message.IsBodyHtml = false; //是否为html格式 
        message.Priority = MailPriority.High; //发送邮件的优先等级 

        SmtpClient sc = new SmtpClient();
        sc.Host = "172.28.1.1"; //指定发送邮件的服务器地址或IP 172.28.1.1 
        sc.Port = 25; //指定发送邮件端口 
        sc.Credentials = new System.Net.NetworkCredential("service@emoney.cn", "1111"); //指定登录服务器的用户名和密码(发件人的邮箱登陆密码)

 
        sc.Send(message); //发送邮件 
 
        return true;

    }
}